package com.sdkteam.MainMenu;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.sdkteam.Ailatrieuphu.Main;
import com.sdkteam.Ailatrieuphu.R;
import com.sdkteam.Database.UpdateDialog;
import com.sdkteam.Help.GameRules;
import com.sdkteam.Others.ControlStatic;
import com.sdkteam.Others.HighScores;

public class MenuActivity extends Activity implements OnClickListener {
	/** Called when the activity is first created. */
	public MediaPlayer mp;
	ProgressDialog waitingDialog;
	Display display;
	private Button play, help, update, exit, highscore, sound;
	final Context context = this;
	boolean check = true;
	Builder builder;
	ControlStatic control;
	Intent intent;
	private com.sdkteam.MainMenu.ScreenFixing size;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu);
		display = getWindowManager().getDefaultDisplay();
		init();
	}

	void init() {
		control = new ControlStatic();

		builder = new AlertDialog.Builder(this);

		play = (Button) findViewById(R.id.imageButton_play);
		play.setOnClickListener(this);

		highscore = (Button) findViewById(R.id.imageButton_highscore);
		highscore.setOnClickListener(this);

		help = (Button) findViewById(R.id.imageButton_help);
		help.setOnClickListener(this);

		update = (Button) findViewById(R.id.imageButton_update);
		update.setOnClickListener(this);

		exit = (Button) findViewById(R.id.imageButton_exit);
		exit.setOnClickListener(this);

		sound = (Button) findViewById(R.id.speaker);
		sound.setOnClickListener(this);
		// resize screen
		size = new ScreenFixing(display);
		size.resizeView(play, 2);		
		size.resizeView(highscore, 2);
		size.resizeView(help, 2);
		size.resizeView(update, 2);
		size.resizeView(exit, 2);
		size.resizeView(sound, 2);
	}

	@SuppressWarnings("deprecation")
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.imageButton_play:
			check = false;
			disableButton();
			if (ControlStatic.Sound)
				startnewMP3(R.raw.ready);
			showDialog(1);
			break;
		case R.id.imageButton_highscore:
			intent = new Intent(context, HighScores.class);
			startActivity(intent);
			break;
		case R.id.speaker:
			if (ControlStatic.Sound) {
				sound.setBackgroundResource(R.drawable.sound_mute);
				ControlStatic.Sound = false;
				mp.release();
			} else {
				ControlStatic.Sound = true;
				sound.setBackgroundResource(R.drawable.button_press_speaker);
				startnewMP3(R.raw.background_music);
				mp.setLooping(true);
			}
			break;
		case R.id.imageButton_help:
			intent = new Intent(context, GameRules.class);
			startActivity(intent);
			break;
		case R.id.imageButton_update:
			disableButton();
			UpdateDialog update = new UpdateDialog(context, this);
			update.show();
			
			break;
		case R.id.imageButton_exit:
			disableButton();
			builder.setMessage(control.setText(context, R.string.WANT_EXIT));
			builder.setPositiveButton(control.setText(context, R.string.YES),
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							finish();
						}
					});
			builder.setNegativeButton(control.setText(context, R.string.NO),
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							enableButton();

						}
					});
			builder.show();
			break;
		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mp != null) {
			mp.release();
		}
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		if (mp != null) {
			mp.release();
		}
		if (waitingDialog != null) {
			waitingDialog.dismiss();
		}

	}

	@Override
	protected void onPause() {
		super.onPause();
		if (mp != null) {
			mp.release();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (!check) {
			// progressPlay();
			showDialog(1);
		}
		if (ControlStatic.Sound) {
			startnewMP3(R.raw.background_music);
			mp.setLooping(true);
		} else {
			sound.setBackgroundResource(R.drawable.sound_mute);
		}
	}

	public void progressPlay() {
		enableButton();
		final Intent i = new Intent(context, Main.class);
		if (ControlStatic.Sound) {
			waitingDialog = ProgressDialog.show(context, "",
					control.setText(context, R.string.MESSAGELOADING));
			startnewMP3(R.raw.gofind);
			mp.setOnCompletionListener(new OnCompletionListener() {

				public void onCompletion(MediaPlayer mp) {
					check = true;
					waitingDialog.dismiss();

					startActivity(i);
				}
			});
		} else {
			enableButton();
			check = true;
			startActivity(i);
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {

		switch (id) {
		case 1:

			builder.setMessage(control.setText(MenuActivity.this,
					R.string.isready));
			builder.setPositiveButton(control.setText(context, R.string.READY),
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							progressPlay();

						}
					});
			builder.setNegativeButton(
					control.setText(context, R.string.NOREADY),
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							check = true;
							enableButton();
							if (ControlStatic.Sound) {
								startnewMP3(R.raw.background_music);
								mp.setLooping(true);
							}
						}
					});

			builder.show();
			break;

		// case 2:
		// builder.setTitle("Cập Nhật Câu Hỏi");
		// builder.setMessage("Gửi tin nhắn tới ****"+"\n"+"Cước phí: 15.000đ");
		// builder.setPositiveButton("Đồng ý",
		// new DialogInterface.OnClickListener() {
		//
		// public void onClick(DialogInterface dialog, int which) {
		//
		// try {
		// Toast.makeText(context, "Sendding SMS...", Toast.LENGTH_LONG).show();
		// dismissDialog(which);
		// } catch (Exception e) {
		// // TODO: handle exception
		// Toast.makeText(context, "Kiểm tra lại tài khoản của bạn",
		// Toast.LENGTH_LONG).show();
		// dismissDialog(which);
		// }
		//
		//
		// }
		// });
		// builder.setNegativeButton(
		// "Không",
		// new DialogInterface.OnClickListener() {
		//
		// public void onClick(DialogInterface dialog, int which) {
		//
		// dismissDialog(which);
		// }
		// });
		//
		// builder.show();
		//
		// break;
		}

		return super.onCreateDialog(id);

	}

	void startnewMP3(int id) {
		if (mp != null) {
			mp.release();
			mp = MediaPlayer.create(this, id);
			mp.start();
		} else {
			mp = MediaPlayer.create(this, id);
			mp.start();
		}

	}

	public void disableButton() {
		play.setClickable(false);
		highscore.setClickable(false);
		help.setClickable(false);
		update.setClickable(false);
		exit.setClickable(false);
	}

	public void enableButton() {
		play.setClickable(true);
		highscore.setClickable(true);
		help.setClickable(true);
		update.setClickable(true);
		exit.setClickable(true);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			builder.setMessage(control.setText(context, R.string.WANT_EXIT));
			builder.setPositiveButton(control.setText(context, R.string.YES),
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							finish();
						}
					});
			builder.setNegativeButton(control.setText(context, R.string.NO),
					null);
			builder.show();
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);
		enableButton();
		// TODO Auto-generated method stub
		return true;
	}

}